package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.PERecipeListAdapter;
import com.glory.dgcpcpda.adapter.WForMaterialListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.NitrCabinetEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class WForMaterialActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.etEqpWorkPosition)
    TextView etEqpWorkPosition;
    @BindView(R.id.etEqpBdName)
    TextView etEqpBdName;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvPEListS)
    RecyclerView rvPEList;

    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;

    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.ivEqpTypeArrow)
    ImageView ivEqpTypeArrow;
    @BindView(R.id.flEqpTypeList)
    FrameLayout flEqpTypeList;

    @BindView(R.id.tvDlType)
    TextView tvDlType;
    @BindView(R.id.ivDlTypeArrow)
    ImageView ivDlTypeArrow;
    @BindView(R.id.dlTypeList)
    FrameLayout dlTypeList;

    @BindView(R.id.etRepNum)
    FreeUI_ClearEditText etRepNum;

    private List<String> mEqpType = new ArrayList<>();
    private FreeUI_GeneralPop mEqpTypePop;
    private String Count="";
    private WForMaterialListAdapter adapter;

    private List<String> mDlType = new ArrayList<>();
    private FreeUI_GeneralPop mDlTypePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_wformaterial;
    }

    @Override
    protected void initView() {
        tvTitle.setText("待料上报");
        setMainLayout(llMain);

        initRecyclerView(rvPEList, this);
        adapter = new WForMaterialListAdapter(null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.isFirstOnly(true);
        rvPEList.setAdapter(adapter);

        listTv1.setText("氮气柜编号");
        listTv2.setText("状态时间");
        listTv3.setVisibility(View.GONE);
        listTv4.setVisibility(View.GONE);

        mEqpType.add("产品批次");
        mEqpType.add("装片胶");
        mEqpType.add("引线框");
        mEqpType.add("计划停机");

        mDlType.add("待料上报");
        mDlType.add("备料上报");
//        requestEqpList();

        initEdit(etEqpWorkID, s -> requestMaterial(s));

        etEqpWorkID.requestFocus();
    }

//    private void requestEqpList() {
//        FreeUI_EntityCallBack<BaseResponseBean<List<String>>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<List<String>>>
//                (new TypeToken<BaseResponseBean<List<String>>>() {
//                }.getType(), getSupportFragmentManager(), this) {
//
//            @Override
//            public void onSuccess(Response<BaseResponseBean<List<String>>> response) {
//                super.onSuccess(response);
//                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
//                    mEqpType = response.body().getResponse().getBody();
//                    mEqpTypePop = null;  // 重置菜单
//                }
//            }
//
//            @Override
//            public void onError(Response<BaseResponseBean<List<String>>> response) {
//                super.onError(response);
//                loadError(response.getException(), "WaitingForMaterialList");
//            }
//        };
//        JsonObject map = new JsonObject();
//        map.addProperty("EQUIPMENTID","");
//        NetUtils.requestPostCPC("WaitingForMaterialList", "WaitingForMaterialList", this, map, callBack);
//    }

    private void requestMaterial(String eqpid) {
        FreeUI_EntityCallBack<BaseResponseBean<NitrCabinetEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<NitrCabinetEntity>>
                (new TypeToken<BaseResponseBean<NitrCabinetEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<NitrCabinetEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    etEqpWorkPosition.setText(response.body().getResponse().getBody().getLOCATION());
                    etEqpBdName.setText(response.body().getResponse().getBody().getBDNAME());
                    List<NitrCabinetEntity.NitrCabList> list = response.body().getResponse().getBody().getLIST();
                    adapter.getData().clear();
                    adapter.addData(list);
                    etRepNum.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<NitrCabinetEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetWaitingForMaterial");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID",eqpid);
        NetUtils.requestPostCPC("GetWaitingForMaterial", "GetWaitingForMaterial", this, map, callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.flEqpTypeList,R.id.dlTypeList})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (getText(etEqpWorkID).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }
                    if(tvEqpType.getText().toString().equals("装片胶")){
                        Count=etRepNum.getText().toString();
                        if(Count.equals("")){
                            showToast("请填写上报的装片胶数量",false);
                            return;
                        }
                       // Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
                        Pattern pattern = Pattern.compile("^\\+?[1-9][0-9]*$");
                        if(pattern.matcher(Count).matches()){

                        }else{
                            showToast("上报数量只能填大于0的整数，请重新填写",false);
                           // etRepNum.setText("");
                            return;
                        }
                    }
                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showToast("上报成功",true);
                            etEqpWorkID.setText("");
                            etEqpWorkPosition.setText("");
                            etEqpWorkID.requestFocus();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"PendingMaterialReporting");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("EQUIPMENTID",getText(etEqpWorkID));
                map.addProperty("REPORTTYPE",tvDlType.getText().toString());
                map.addProperty("REPORTNAME",tvEqpType.getText().toString());
                map.addProperty("REPORTUSER", Constant.CURRENT_USER);
                map.addProperty("Count",Count);
                NetUtils.requestPostNet("CPC.PendingMaterialReporting","PendingMaterialReporting",this,map,callBack);
                break;
            case R.id.flEqpTypeList:
                if (mEqpType.size() <= 0) {
                    showToast("未获取到上报类型列表", false);
                    return;
                }

                if (mEqpTypePop != null) {
                    if (mEqpTypePop.isShowing()) {
                        mEqpTypePop.dismiss();
                    }
                } else {
                    mEqpTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flEqpTypeList.getWidth(), mEqpType, ivEqpTypeArrow, (codeS, i) -> {
                        tvEqpType.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivEqpTypeArrow);
                mEqpTypePop.showAsDropDown(flEqpTypeList, 0, 0);
                break;
            case R.id.dlTypeList:
                if (mDlType.size() <= 0) {
                    showToast("未获取到上报类型列表", false);
                    return;
                }

                if (mDlTypePop != null) {
                    if (mDlTypePop.isShowing()) {
                        mDlTypePop.dismiss();
                    }
                } else {
                    mDlTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), dlTypeList.getWidth(), mDlType, ivDlTypeArrow, (codeS, i) -> {
                        tvDlType.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivDlTypeArrow);
                mDlTypePop.showAsDropDown(dlTypeList, 0, 0);
                break;
        }

    }

}
